/**************************************************************************************************
  Filename:       communicate.h
  Revised:        $Date: 2012-02-12 15:58:41 -0800 (Sun, 12 Feb 2012) $
  Revision:       $Revision: 29216 $

  Description:    This file contains the Generic Application definitions.


  Copyright 2004-2012 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License"). You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product. Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED �AS IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com. 
**************************************************************************************************/

#ifndef COMMUNICATE_H
#define COMMUNICATE_H

#ifdef __cplusplus
extern "C"
{
#endif

/*********************************************************************
 * INCLUDES
 */
#include "ZComDef.h"
#include "hal_uart.h"
/*********************************************************************
 * CONSTANTS
 */

#define HTAG ('#')
#define TTAG ('*')
//#define CCHD	(0x01) //control node heart command control node --> cooperation.
#define HDHD	(0x02) // heart command  switch/control node --> cooperation
//#define ACK		(0x82)
#define LCTRL	(0x03) //master light control command  gate ware to node
#define LACK	(0x83) //light control ACK
#define PREG	(0x04) //argument register command : gateware to node
#define PACK	(0x84) //argument register ACK
#define TREG	(0x05) //node register command
#define GACK	(0x85) //register ACK
#define CTIME	(0x06) //meet time command : gateware to node
#define TACK	(0x86) //meet time command ACK
#define RREG	(0x07) //again register command : gateware to node
#define RACK	(0x87) //again register command ACK
#define DREG	(0x08) //device register command : gateware to node
#define DACK	(0x88) //device register command ACK

/////
#define NODE_LIGHT_MAX (8)

enum ERROR {
	ESUCESS,
	EUNVALID,
	EPNULL,
    EPACKET
};

struct light {

	uint16 light_fixed;
	uint16 light_up;
	uint16 light_down;
	uint16 def_luma;
	uint16 level;
    uint16 sub_devid;
	
};

struct sensor {
	uint8 type;

};

//switch node heart command light data
typedef struct CswNode_t {
	uint16 luma;
	uint16 fixed_luma;
	uint16 on_luma;
	uint16 off_luma;
	uint16 voltage;
	uint16 perm; //permission H byte ,human sensor , L byte , light sensor
	//uint16 node_type; //u8 is ok
	uint16 dev_id;
	uint16 gid;
}CswNode, *pCswNode;

//control node heart command data
typedef struct CcltNode_t {
	uint8 hsensor; //H 4bits : interface 1 , L 4bits : interface 2 if heart sensor 2bits
	uint8 dsensor; //distance sensor
	uint8 lamp_num; //all node lamps
	struct light lamp[2];
	uint16 voltage;
	uint16 dev_id;
	uint16 gid;
	//uint16 node_type; //u8 is ok
}CctlNode, *pCctlNode;

struct ttime {
	uint8 hour;
	uint8 min;
	uint8 sec;
};

typedef struct Cheader_stu {
	uint8 type; //command type
    uint16 length; // package data payload length 
	uint8 hash; //hash verify codes	
	uint16 counter;
	//struct ttime time;	
	//uint16 dev_id;
	//uint16 gid;

}Cheader, *pCheader;

//master light control command argument.
typedef struct CctlLight_t {
	uint16 dev_id;
	uint16 gid;
	struct light  lamp; //for each light by lamp.sub_devid
	//uint8  lamp[8]; //for 8 light
}CctrlCmd, *pCctrCmd;
//node or again register command argument
typedef struct CnodeReg_t {	
	uint16 dev_id;
	uint16 gid;	
	uint8  ieee[8];
}CnodeReg, *pCnodeReg;


//device register command argument
typedef struct CdevReg_t {

	uint16 dev_type;
	uint16 dev_id;
	uint16 gid;
	uint16 sub_id;
}CdevReg, *pCdevReg;

// argument register command argument.
typedef struct CconfArg_t {
	uint16 dev_id;
	uint16 gid;
	uint16 dev_type;
	uint8 dev_mode;
	uint32 network_id;
	uint32 key;
	struct light light_para[8];
	struct sensor sensor_para[2];
}CconfArg, *pCconfArg;	
//meet time command argument
typedef struct Cctime_t {
	struct ttime time; //h:m:s
}Cctime, *pCctime;

typedef struct Ccmd_stu {
	union cmd_un {
		CswNode swNodeInfo;
		CctlNode ctlNodeInfo;
		CctrlCmd ctrlCmdInfo;
		CnodeReg nodeRegInfo;
		CdevReg  devRegInfo;
		CconfArg confArgInfo;
		Cctime	 ctimeInfo;
	}cmd_un;
}Ccmd, *pCcmd;

typedef struct Cpacket {
	Cheader header; //the command type in header field
	Ccmd cdata;//command argument.
}Cpacket, *pCpacket;

/*********************************************************************
*********************************************************************/
uint16 RecivePackets(afIncomingMSGPacket_t *pkt);
uint16 SendHeardCommand(afAddrType_t dstAddr, endPointDesc_t epDesc, uint16 cID, uint8 * transID);
uint16 SendLightCtrlCommand(afAddrType_t dstAddr, endPointDesc_t epDesc, uint16 cID, uint8 *transID);

#define UartDebug(str) do { \
		HalUARTWrite(0,str, sizeof(str)); \
	}while(0)

#ifdef __cplusplus
}
#endif

#endif
